<style>
  .layui-form {
    margin-left: 15px;
  }
  .layui-form-select .layui-input {
    padding-right: 0
  }
</style>

<!--页面主要内容-->
<main class="lyear-layout-content">

  <div class="container-fluid p-t-15">

    <div class="row">
      <div class="col-lg-12">
        <div class="card">
          <span class="layui-breadcrumb">
            <a href="">首页</a>
            <a href="">问答管理</a>
            <a><cite>帖子管理</cite></a>
          </span>
          <div class="card-toolbar d-flex flex-column flex-md-row">
            <div class="toolbar-btn-action">
              <!-- <a class="btn btn-success m-r-5 ajax-post confirm"  target-form="ids"><i class="mdi mdi-check"></i> 启用</a> -->
              <!-- <a class="btn btn-warning m-r-5" href="#!"><i class="mdi mdi-block-helper"></i> 禁用</a> -->
              <a class="btn btn-danger delAll"><i class="mdi mdi-window-close"></i> 批量删除</a>
            </div>

            <form class="layui-form" id="layui-form" method="get" role="form" style="display: flex;">
              <input type="hidden" name="action" value="search">
              <select name="search" lay-verify="">
                <option value="title">标题</option>
                <option value="nickname">作者</option>
                <option value="cate">分类</option>
              </select>   
              <input type="text" name="title" placeholder="请选择搜索条件" autocomplete="off" class="layui-input">  
              <button class="layui-btn layui-btn-primary" type="submit">搜索</button>
            </form>
          </div>
          <div class="card-body">
            <div class="table-responsive">
              <table class="table table-bordered">
                <thead>
                  <tr>
                    <th>
                      <div class="custom-control custom-checkbox">
                        <input type="checkbox" class="custom-control-input" id="check-all">
                        <label class="custom-control-label" for="check-all"></label>
                      </div>
                    </th>
                    <th>编号</th>
                    <th>标题</th>
                    <th>作者</th>
                    <th>内容</th>
                    <th>积分</th>
                    <th>所属分类</th>
                    <th>浏览量</th>
                    <th>创建时间</th>
                    <th>状态</th>
                    <th>操作</th>
                  </tr>
                </thead>
                <tbody>
                  {foreach $PostList as $key => $item}
                  <tr>
                    <td>
                      <div class="custom-control custom-checkbox">
                        <input type="checkbox" class="custom-control-input ids" name="ids[]" value="{$item.id}" id="{$item.id}">
                        <label class="custom-control-label" for="{$item.id}"></label>
                      </div>
                    </td>
                    <td>{$key+1}</td>
                    <!-- <td><a href="{:url('home/user/index', ['userid' => $item.userid])}">{$item.title}</a></td> -->
                    <td><a>{$item.title}</a></td>
                    <td>{$item.user.nickname}</td>
                    <td>{$item.content}</td>
                    <td>{$item.point}</td>
                    <td>{$item.cate.name}</td>
                    <td>{$item.visit_count}</td>
                    <td>{$item.createtime}</td>
                    <td>
                      {if condition="$item.state==0"}
                      <button class="layui-btn layui-btn-primary layui-btn-sm">正常</button>
                      {elseif condition="$item.state==1"}
                      <button class="layui-btn layui-btn-warm layui-btn-sm">置顶</button>
                      {elseif condition="$item.state==2"}
                      <button class="layui-btn layui-btn-danger layui-btn-sm">精华</button>
                      {elseif condition="$item.state==3"}
                      <button class="layui-btn layui-btn-normal layui-btn-sm">热门</button>
                      {/if}
                      {if condition="$item.accept>0"}
                      <button class="layui-btn layui-btn-warm layui-btn-sm">已采纳</button>
                      {else /}
                      <button class="layui-btn layui-btn-success layui-btn-sm">未采纳</button>
                      {/if}
                    </td>
                    <td>
                      <div class="btn-group">
                        <a class="btn btn-xs btn-default" href="{:url('home/index/info', ['postid' => $item.id])}" title="" data-toggle="tooltip"
                          data-original-title="帖子详情" target="_black"><i class="mdi mdi-pencil"></i></a>
                        <a class="btn btn-xs btn-default del" title="" data-toggle="tooltip" data-id="{$item.id}"
                        data-original-title="删除"><i class="mdi mdi-window-close"></i></a>
                      </div>
                    </td>
                  </tr>
                  {/foreach}
                </tbody>
              </table>
            </div>
            
            <!-- 分页 -->
            {$PostList->render()}

          </div>
        </div>
      </div>

    </div>

  </div>

</main>
<!--End 页面主要内容-->
<script>
  layui.use(['layer'], function () {
    var $ = layui.jquery,
      layer = layui.layer

      // 删除
    $('.del').click(function () {
      let id = $(this).data('id');
      console.log(id)
      layer.confirm('确定删除该帖子？', { icon: 3, title: '提示' }, function (index) {
        // 发起请求
        $.ajax({
          type: 'post',
          url: `{:url('admin/post/del')}`,
          data: {
            id
          },
          dataType: 'json',
          success: function (res) {
            if (res.code == 1) {
              layer.msg(res.msg, { icon: 1, time: 1500 }, function (index) {
                // location.href = res.url
                location.reload()
                layer.close(index)
              })
            } else {
              layer.msg(res.msg, { icon: 2, time: 1500 }, function (index) {
                layer.close(index)
                // location.reload()
              })
            }
          }
        })
        layer.close(index)
      })
    })

    function GetId () {
      // 存放id的数组
      let list = []
      $('input[name="ids[]"]:checked').each(function () {
        // console.log(item);
        list.push($(this).val())
      })
      return list
    }

    // 批量删除
    $('.delAll').click(function () {
      layer.confirm('确定批量删除？', { icon: 3, title: '提示' }, function (index) {
        let list = GetId()
        if (list.length == 0) {
          // alert("至少选择一条帖子!")
          layer.alert('至少选择一条帖子!')
          return false
        }
        // console.log(list)
        $.ajax({
          type: 'post',
          url: `{:url('admin/post/delAll')}`,
          data: {
            list,
            action: 'delAll'
          },
          dataType: 'json',
          success: function (res) {
            if (res.code == 1) {
              layer.alert(res.msg, function (index) {
                // location.reload()
                location.href = res.url
                layer.close(index);
              });
            } else {
              layer.alert(res.msg, function (index) {
                location.reload()
                layer.close(index);
              });
            }
          }
        })
        layer.close(index)
      })
    })

    // 批量启用
    $('#stateup').click(function () {
      let res = confirm('确认批量启用？')
      if (!res) {
        return false
      }
      let list = GetId()
      if (list.length == 0) {
        alert("至少选择一名管理员!")
        return false
      }
      console.log(list)
      $.ajax({
        type: 'post',
        url: `{:url('admin/admin/stateUp')}`,
        data: {
          list,
          action: 'stateup'
        },
        dataType: 'json',
        success: function (res) {
          if (res.code == 1) {
            layer.msg(res.msg, { icon: 1, time: 1500 }, function (index) {
              // location.href = res.url
              layer.close(index)
              location.reload()
            })
          } else {
            layer.msg(res.msg, { icon: 1, time: 1500 }, function (index) {
              // location.href = res.url
              layer.close(index)
              location.reload()
            })
          }
        },
      })
    })

    // 批量禁用
    $('#statedown').click(function () {
      let res = confirm('确认批量禁用？')
      if (!res) {
        return false
      }
      let list = GetId()
      if (list.length == 0) {
        alert("至少选择一名管理员!")
        return false
      }
      console.log(list)
      $.ajax({
        type: 'post',
        url: `{:url('admin/admin/stateDown')}`,
        data: {
          list,
          action: 'stateDown'
        },
        dataType: 'json',
        success: function (res) {
          if (res.code == 1) {
            layer.msg(res.msg, { icon: 1, time: 1500 }, function (index) {
              // location.href = res.url
              layer.close(index)
              location.reload()
            })
          } else {
            layer.msg(res.msg, { icon: 1, time: 1500 }, function (index) {
              // location.href = res.url
              layer.close(index)
              location.reload()
            })
          }
        },
      })
    })
  })

</script>